<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\game\Record;
use think\Db;

class Invite extends Api
{
    protected $noNeedLogin = "";
    protected $noNeedRight = "*";

    public function index()
    {
        $time = $this->request->param("time", 0);

        $where = [];
        $length = strlen($time);
        if ($time != 0) {
            $begin = strtotime($time);
            if ($length == 10) {
                $where["createtime"] = ["between", [$begin, $begin + 86400]];
            }

            if ($length == 7) {
                $where["createtime"] = ["between", [$begin, strtotime("next month", $begin)]];
            }
        }

        $child = Db::name('user')->where('parent_id', $this->auth->id)->column('id');//邀请所有人

        $user_num = Db::name('user')->where('parent_id', $this->auth->id)->where($where)->count();//新增
        $take_num = Db::name('game_record')->group("user_id")->where(["user_id" => ["in", $child]])
            ->where($where)->count();
        $entries_num = Db::name('game_record')->where(["user_id" => ["in", $child]])->where($where)->count();
        $vip_num = Db::name('user_vip_log')->group("user_id")->where(["user_id" => ["in", $child]])
            ->where($where)->count();

        $service_charge = Db::name('user_diamond_log')->where(["user_id" => $this->auth->id, "type" => ["in", [1, 3]]])->where($where)->sum("diamond");
        $vip_shouyi = Db::name('user_diamond_log')->where(["user_id" => $this->auth->id, "type" => 5])->where($where)->sum("diamond");

        $club_id = 0;
        if ($this->auth->is_club == 1) {
            $club_id = $this->auth->id;
        } else {
            if (!empty($this->auth->parent_id)) {
                $club_id = Db::name('user')->where('id', $this->auth->parent_id)->where('is_club', 1)->value('id', 0);
            }
        }

        $club_info = Db::name('user_club')->where('user_id', $club_id)->find();

        $data = [
            'user_num' => $user_num,//新增玩家
            'take_num' => $take_num,//参数玩家
            'entries_num' => $entries_num,//参赛次数
            'service_charge' => $service_charge,//服务费
            'vip_num' => $vip_num,//开会员人数
            'vip_divide' => $vip_shouyi,
            'club_info' => $club_info
//            'num' => $length//7是月份 10是日期
        ];

        $this->success("", $data);
    }

    public function summary()
    {
        $all = Db::name("user_diamond_log")->where("user_id", $this->auth->id)->where("type", "in", ["1", "3"])->sum("diamond", 0);
        $today = Db::name("user_diamond_log")->where("user_id", $this->auth->id)->where("type", "in", ["1", "3"])->whereTime("createtime", "today")->sum("diamond", 0);
        $month = Db::name("user_diamond_log")->where("user_id", $this->auth->id)->where("type", "in", ["1", "3"])->whereTime("createtime", "month")->sum("diamond", 0);
        $this->success("", [
            "all" => $all,//总收益
            "today" => $today,//今日收益
            "month" => $month//当月收益
        ]);
    }

    public function gamelog()
    {
        $id = $this->request->param("id", 0);
        $pagesize = $this->request->param("size", 10);
        $child = Db::name('user')->where('parent_id', $this->auth->id)->column('id');//邀请所有人
        $where = [];
        if ($id != 0 && in_array($id, $child)) {
            $where['gr.user_id'] = $id;
        }

        $lists = Db::name('game_record')->alias('gr')
            ->join("user u", "u.id = gr.user_id")
            ->join("game_lists gl", "gl.id = gr.game_id")
            ->join("game_modes gm", "gm.id = gr.mode_id")
            ->where(["gr.user_id" => ["in", $child]])->where($where)
            ->field("gr.*,u.nickname,gl.name as game_name,gm.name as mode_name,gr.rank as rank_num")->paginate($pagesize);

//        Record::where(["user_id" => ["in", $child]])->where($where)->paginate();
//        $this->success("", [
//            ['id' => 1, 'nickname' => 111, 'game_name' => '金铲铲', 'mode_name' => '狂暴模式', 'rank_num' => 2]
//        ]);
        $this->success("", $lists);
    }

    public function userlog()
    {
        $pagesize = $this->request->param("size", 10);
        $lists = \app\common\model\User::where('parent_id', $this->auth->id)
            ->field("id,nickname,avatar,mobile,createtime")->paginate($pagesize);
        $this->success("", $lists);
    }


}